from common.base_api import BaseApi
from common.wrapper import api_call
from common.utils import Utils
from requests import api
from requests.api import get
from requests.models import Response

class AdminApi(BaseApi):

    # 前置条件--登陆态 self.get_Authorization
    
    @api_call
    def banner_list_api(self, type:str):
        """
        banner列表查询
        :return:
        """
        api = self.conf_data['api']['admin']['banner_list']
        data = {
            'url': self.host + api,
            'method': 'get',
            'headers': self.headers,
            'params': {
                'type' : type
            }
        }
        response = self.send_http(data)
        return response

    @api_call
    def banner_api(self, type:str, pageType:str, background:str):
        """
        新增banner
        """
        headers = self.headers
        headers['Authorization'] = self.get_Authorization
        api = self.conf_data['api']['admin']['banner']
        data = {
            'url': self.host + api,
            'method': 'post',
            'headers': headers,
            'json': {
                'type' : type,
                'pageType' : pageType,
                'background' : background
            }
        }
        response = self.send_http(data)
        return response

    @api_call
    def banner_id_api(self, pageType:str, type:str, id:str):
        """
        修改banner
        """
        headers = self.headers
        headers['Authorization'] = self.get_Authorization
        api = self.conf_data['api']['admin']['banner_id']
        data = {
            'url': self.host + api + str(id),
            'method': 'put',
            'headers': headers,
            'json': {
                'type' : type,
                'pageType' : pageType
            }
        }
        response = self.send_http(data)
        return response
    
    @api_call
    def adjustment_order_api(self, id:str, move:str):
        """
        banner调整顺序
        """
        headers = self.headers
        headers['Authorization'] = self.get_Authorization
        api = self.conf_data['api']['admin']['adjustment_order']
        index = api.find('?')
        api = api[:index] + id + api[index+1:]
        data = {
            'url': self.host + api + move,
            'method': 'put',
            'headers': headers,
            'json': {
                    }
                }
        response = self.send_http(data)
        return response

    @api_call
    def banner_delete_api(self, id:str):
        """
        删除banner
        """
        headers = self.headers
        headers['Authorization'] = self.get_Authorization
        api = self.conf_data['api']['admin']['banner_delete']
        data = {
            'url': self.host + api + str(id),
            'method': 'delete',
            'headers': headers,
            'json': {
                    }
                }
        response = self.send_http(data)
        return response

    @api_call
    def balance_withdraw_list_api(self, auditStateList:str, userId:int):
        """
        余额提现列表
        """
        headers = self.headers
        headers['Authorization'] = self.get_Authorization
        api = self.conf_data['api']['admin']['balance_withdraw_list']
        data = {
            'url': self.host + api,
            'method': 'get',
            'headers': headers,
            'params': {
                    'auditStateList' : auditStateList,
                    'userId' : userId
                    }
                }
        response = self.send_http(data)
        return response

    @api_call
    def balance_audit_withdraw_id_api(self, id:str, auditState:str, auditAdvice:str="审核意见测试"):
        """
        余额提现审核
        """
        headers = self.headers
        headers['Authorization'] = self.get_Authorization
        api = self.conf_data['api']['admin']['balance_audit_withdraw_id']
        data = {
            'url': self.host + api + str(id),
            'method': 'post',
            'headers': headers,
            'json': {
                    'auditState' : auditState,
                    'auditAdvice' : auditAdvice
                    }
                }
        response = self.send_http(data)
        return response

    @api_call
    def bank_show_cardno_api(self, id:str):
        """
        查看银行卡卡号
        """
        headers = self.headers
        headers['Authorization'] = self.get_Authorization
        api = self.conf_data['api']['admin']['bank_show_cardno']
        data = {
            'url': self.host + api,
            'method': 'get',
            'headers': headers,
            'params': {
                    'id' : id
                    }
                }
        response = self.send_http(data)
        return response

    @api_call
    def balance_withdraw_id_api(self, id:str):
        """
        余额提现记录详情
        """
        headers = self.headers
        headers['Authorization'] = self.get_Authorization
        api = self.conf_data['api']['admin']['balance_withdraw_id']
        data = {
            'url': self.host + api + str(id),
            'method': 'get',
            'headers': headers,
            'json': {
                    }
                }
        response = self.send_http(data)
        return response

    @api_call
    def productAudit_list_api(self, pageSize:int, pageNumber:int, name:str, examineStatus:str):
        """
        审核列表
        """
        headers = self.headers
        headers['Authorization'] = self.get_Authorization
        api = self.conf_data['api']['admin']['productAudit_list']
        data = {
            'url': self.host + api,
            'method': 'get',
            'headers': headers,
            'params': {
                        'pageSize' : pageSize,
                        'pageNumber' : pageNumber,
                        'name' : name,
                        'examineStatus' : examineStatus
                      }
                }
        response = self.send_http(data)
        return response

    @api_call
    def productAudit_detail_api(self, id:int):
        """
        审核详情
        """
        headers = self.headers
        headers['Authorization'] = self.get_Authorization
        api = self.conf_data['api']['admin']['productAudit_detail']
        data = {
            'url': self.host + api,
            'method': 'get',
            'headers': headers,
            'params': {
                        'id' : id
                      }
                }
        response = self.send_http(data)
        return response

    @api_call
    def productAudit_approved_api(self, id:int, remark:str):
        """
        审核通过
        """
        headers = self.headers
        headers['Authorization'] = self.get_Authorization
        api = self.conf_data['api']['admin']['productAudit_approved']
        data = {
            'url': self.host + api,
            'method': 'post',
            'headers': headers,
            'json': {
                        'id' : id,
                        'remark' : remark
                    }
                }
        response = self.send_http(data)
        return response 

    @api_call
    def productAudit_refused_api(self, id:int, remark:str):
        """
        审核通过
        """
        headers = self.headers
        headers['Authorization'] = self.get_Authorization
        api = self.conf_data['api']['admin']['productAudit_refused']
        data = {
            'url': self.host + api,
            'method': 'post',
            'headers': headers,
            'json': {
                        'id' : id,
                        'remark' : remark
                    }
                }
        response = self.send_http(data)
        return response 

    @api_call
    def productreport_list_api(self, pageSize:int, pageNumber:int):
        """
        举报列表
        """
        headers = self.headers
        headers['Authorization'] = self.get_Authorization
        api = self.conf_data['api']['admin']['productreport_list']
        data = {
            'url': self.host + api,
            'method': 'get',
            'headers': headers,
            'params': {
                        'pageNumber' : pageNumber,
                        'pageSize' : pageSize
                    }
                }
        response = self.send_http(data)
        return response

    @api_call
    def productreport_approved_api(self, id:int, remark:str):
        """
        举报审核通过
        """
        headers = self.headers
        headers['Authorization'] = self.get_Authorization
        api = self.conf_data['api']['admin']['productreport_approved']
        data = {
            'url': self.host + api,
            'method': 'post',
            'headers': headers,
            'json': {
                    'id' : id,
                    'remark' : remark
                    }
                }
        response = self.send_http(data)
        return response

    @api_call
    def productreport_refused_api(self, id:int, remark:str):
        """
        举报审核拒绝
        """
        headers = self.headers
        headers['Authorization'] = self.get_Authorization
        api = self.conf_data['api']['admin']['productreport_refused']
        data = {
            'url': self.host + api,
            'method': 'post',
            'headers': headers,
            'json': {
                    'id' : id,
                    'remark' : remark
                    }
                }
        response = self.send_http(data)
        return response

    @api_call
    def appversion_page_api(self, pageNumber:int, pageSize:int, appName:str, appType:str):
        """
        APP版本列表查询
        """
        headers = self.headers
        headers['Authorization'] = self.get_Authorization
        api = self.conf_data['api']['admin']['appversion_page']
        data = {
            'url': self.host + api,
            'method': 'get',
            'headers': headers,
            'params': {
                    'pageNumber' : pageNumber,
                    'pageSize' : pageSize,
                    'appName' : appName,
                    'appType' : appType
                    }
                }
        response = self.send_http(data)
        return response

    @api_call
    def appversion_list_api(self, version:str, appName:str, appType:str):
        """
        APP版本check
        """
        headers = self.headers
        headers['Authorization'] = self.get_Authorization
        api = self.conf_data['api']['admin']['appversion_list']
        data = {
            'url': self.host + api,
            'method': 'get',
            'headers': headers,
            'params': {
                    'version' : version,
                    'appName' : appName,
                    'appType' : appType
                    }
                }
        response = self.send_http(data)
        return response
    
    @api_call
    def appversion_add_api(self, appName:str, appType:str, version:str, updateDesc:str, updateType:str, file):
        """
        APP版本check
        """
        headers = self.headers
        headers['Authorization'] = self.get_Authorization
        api = self.conf_data['api']['admin']['appversion_add']
        data = {
            'url': self.host + api,
            'method': 'post',
            'headers': headers,
            'json': {
                    'appName' : appName,
                    'appType' : appType,
                    'version' : version,
                    'updateDesc' : updateDesc,
                    'updateType' : updateType,
                    'file' : file
                    }
                }
        response = self.send_http(data)
        return response

    @api_call
    def appversion_detail_api(self, id:int):
        """
        APP版本详情
        """
        headers = self.headers
        headers['Authorization'] = self.get_Authorization
        api = self.conf_data['api']['admin']['appversion_detail']
        data = {
            'url': self.host + api + str(id),
            'method': 'get',
            'headers': headers,
            'params': {
                    }
                }
        response = self.send_http(data)
        return response

    @api_call
    def userrate_list_api(self, pageNumber:int, pageSize:int):
        """
        用户平台服务费列表
        """
        headers = self.headers
        headers['Authorization'] = self.get_Authorization
        api = self.conf_data['api']['admin']['userrate_list']
        data = {
            'url': self.host + api,
            'method': 'get',
            'headers': headers,
            'params': {
                    'pageNumber' : pageNumber,
                    'pageSize' : pageSize
                    }
                }
        response = self.send_http(data)
        return response
    
    @api_call
    def userrate_detail_api(self, id:int):
        """
        用户平台服务费详情
        """
        headers = self.headers
        headers['Authorization'] = self.get_Authorization
        api = self.conf_data['api']['admin']['userrate_detail']
        data = {
            'url': self.host + api + str(id),
            'method': 'get',
            'headers': headers,
            'params': {
                    }
                }
        response = self.send_http(data)
        return response

    @api_call
    def userrate_add_api(self, userId:int, serviceRate:str):
        """
        用户平台服务费新增
        """
        headers = self.headers
        headers['Authorization'] = self.get_Authorization
        api = self.conf_data['api']['admin']['userrate_add']
        data = {
            'url': self.host + api,
            'method': 'post',
            'headers': headers,
            'json': {
                    'userId' : userId,
                    'serviceRate' : serviceRate
                    }
                }
        response = self.send_http(data)
        return response

    @api_call
    def userrate_update_api(self, id:str, serviceRate:str):
        """
        用户平台服务费修改
        """
        headers = self.headers
        headers['Authorization'] = self.get_Authorization
        api = self.conf_data['api']['admin']['userrate_update']
        data = {
            'url': self.host + api,
            'method': 'post',
            'headers': headers,
            'json': {
                    'id' : id,
                    'serviceRate' : serviceRate
                    }
                }
        response = self.send_http(data)
        return response

    @api_call
    def queryInvitation_api(self, pageNumber:int, pageSize:int):
        """
        邀请总览查询
        """
        headers = self.headers
        headers['Authorization'] = self.get_Authorization
        api = self.conf_data['api']['admin']['queryInvitation']
        data = {
            'url': self.host + api,
            'method': 'get',
            'headers': headers,
            'params': {
                    'pageNumber' : pageNumber,
                    'pageSize' : pageSize
                    }
                }
        response = self.send_http(data)
        return response

    @api_call
    def queryInvitationItem_api(self, pageNumber:int, pageSize:int):
        """
        邀请明细查询
        """
        headers = self.headers
        headers['Authorization'] = self.get_Authorization
        api = self.conf_data['api']['admin']['queryInvitationItem']
        data = {
            'url': self.host + api,
            'method': 'get',
            'headers': headers,
            'params': {
                    'pageNumber' : pageNumber,
                    'pageSize' : pageSize
                    }
                }
        response = self.send_http(data)
        return response

    @api_call
    def exportExcel_api(self):
        """
        导出表格(总览)
        """
        headers = self.headers
        headers['Authorization'] = self.get_Authorization
        api = self.conf_data['api']['admin']['exportExcel']
        data = {
            'url': self.host + api,
            'method': 'get',
            'headers': headers
                }
        import requests
        from loguru import logger
        try:
            response = requests.request(**data)
            logger.info(f"响应状态码为：{response.status_code}")
        except Exception as e:
            logger.error(f'发送请求失败，请求参数为：{data}')
            logger.exception(f'发生的错误为：{e}')
            raise e
        else:
            return response

    @api_call
    def exportItemExcel_api(self):
        """
        导出表格(明细)
        """
        headers = self.headers
        headers['Authorization'] = self.get_Authorization
        api = self.conf_data['api']['admin']['exportItemExcel']
        data = {
            'url': self.host + api,
            'method': 'get',
            'headers': headers
                }
        import requests
        from loguru import logger
        try:
            response = requests.request(**data)
            logger.info(f"响应状态码为：{response.status_code}")
        except Exception as e:
            logger.error(f'发送请求失败，请求参数为：{data}')
            logger.exception(f'发生的错误为：{e}')
            raise e
        else:
            return response

    @api_call
    def userStatistics_api(self):
        """
        数据统计
        """
        headers = self.headers
        headers['Authorization'] = self.get_Authorization
        api = self.conf_data['api']['admin']['exportItemExcel']
        data = {
            'url': self.host + api,
            'method': 'get',
            'headers': headers
                }
        import requests
        from loguru import logger
        try:
            response = requests.request(**data)
            logger.info(f"响应状态码为：{response.status_code}")
        except Exception as e:
            logger.error(f'发送请求失败，请求参数为：{data}')
            logger.exception(f'发生的错误为：{e}')
            raise e
        else:
            return response

    @api_call
    def qlf_shop_api(self, shopTitle:str, shopName:str, shopProfile:str, userId:str, shopBannerUrl:str, ruleDesc:str, shopStatus:str):
        """
        新增店铺信息
        """
        headers = self.headers
        headers['Authorization'] = self.get_Authorization
        api = self.conf_data['api']['admin']['qlf_shop']
        data = {
            'url': self.host + api,
            'method': 'post',
            'headers': headers,
            'json' : {
                    "shopTitle" : shopTitle,
                    "shopName" : shopName,
                    "shopProfile" : shopProfile,
                    "userId" : userId,
                    "shopBannerUrl" : shopBannerUrl,
                    "ruleDesc" : ruleDesc,
                    "shopStatus" : shopStatus
                    }
                }
        response = self.send_http(data)
        return response

    @api_call
    def qlf_shop_list_api(self):
        """
        店铺列表查询
        """
        headers = self.headers
        headers['Authorization'] = self.get_Authorization
        api = self.conf_data['api']['admin']['qlf_shop_list']
        data = {
            'url': self.host + api,
            'method': 'get',
            'headers': headers
                }
        response = self.send_http(data)
        return response

    @api_call
    def qlf_shop_id_api(self, id:int, userId:int, shopTitle:str, shopProfile:str, shopBannerUrl:str, shopExchangeUrl:str, ruleDesc:str, shopStatus:str, shopName:str):
        """
        修改店铺信息
        """
        headers = self.headers
        headers['Authorization'] = self.get_Authorization
        api = self.conf_data['api']['admin']['qlf_shop_id']
        data = {
            'url': self.host + api + str(id),
            'method': 'put',
            'headers': headers,
            'json' : {
                    "userId" : userId,
                    "shopTitle" : shopTitle,
                    "shopProfile" : shopProfile,
                    "shopBannerUrl" : shopBannerUrl,
                    "shopExchangeUrl" : shopExchangeUrl,
                    "shopBannerUrl" : shopBannerUrl,
                    "ruleDesc" : ruleDesc,
                    "shopStatus" : shopStatus,
                    "shopName" : shopName
                    }
                }
        response = self.send_http(data)
        return response

    @api_call
    def qlf_shop_status_api(self, id:int, status:str):
        """
        禁用/启用店铺
        """
        headers = self.headers
        headers['Authorization'] = self.get_Authorization
        api = self.conf_data['api']['admin']['qlf_shop_id']
        data = {
            'url': self.host + api + str(id) + "/status/" + status,
            'method': 'put',
            'headers': headers
                }
        response = self.send_http(data)
        return response

    @api_call
    def qlf_shop_id_info_api(self, id:int):
        """
        店铺详情查询
        """
        headers = self.headers
        headers['Authorization'] = self.get_Authorization
        api = self.conf_data['api']['admin']['qlf_shop_id']
        data = {
            'url': self.host + api + str(id),
            'method': 'get',
            'headers': headers
                }
        response = self.send_http(data)
        return response

    @api_call
    def qlf_shop_productlist_api(self, id:int):# /mp/admin/qlf/shop/{id}/product/list
        """
        店铺商品列表
        """
        headers = self.headers
        headers['Authorization'] = self.get_Authorization
        api = self.conf_data['api']['admin']['qlf_shop_productlist']
        index = api.find('?')
        api = api[:index] + str(id) + api[index+1:]
        data = {
            'url': self.host + api,
            'method': 'get',
            'headers': headers
                }
        response = self.send_http(data)
        return response

    @api_call
    def qlf_shop_productid_api(self, id:int):
        """
        店铺商品列表
        """
        headers = self.headers
        headers['Authorization'] = self.get_Authorization
        api = self.conf_data['api']['admin']['qlf_shop_productid']
        index = api.find('?')
        api = api[:index] + str(id)
        data = {
            'url': self.host + api,
            'method': 'get',
            'headers': headers
                }
        response = self.send_http(data)
        return response

    @api_call
    def qlf_shop_category_api(self, categoryName:str, shopId:int):
        """
        新增店铺分类
        """
        headers = self.headers
        headers['Authorization'] = self.get_Authorization
        api = self.conf_data['api']['admin']['qlf_shop_category']
        data = {
            'url': self.host + api,
            'method': 'post',
            'headers': headers,
            'json' : {
                    'categoryName' : categoryName,
                    'shopId' : shopId
                    }
                }
        response = self.send_http(data)
        return response

    @api_call
    def qlf_shop_categorylist_api(self, id:int):
        """
        查询店铺分类
        """
        headers = self.headers
        headers['Authorization'] = self.get_Authorization
        api = self.conf_data['api']['admin']['qlf_shop_categorylist']
        index = api.find('?')
        api = api[:index] + str(id) + api[index+1:]
        data = {
            'url': self.host + api,
            'method': 'get',
            'headers': headers
                }
        response = self.send_http(data)
        return response

    @api_call
    def qlf_shop_categoryDelete_api(self, id:int):
        """
        删除店铺分类
        """
        headers = self.headers
        headers['Authorization'] = self.get_Authorization
        api = self.conf_data['api']['admin']['qlf_shop_categoryDelete']
        index = api.find('?')
        api = api[:index] + str(id) + api[index+1:]
        data = {
            'url': self.host + api,
            'method': 'delete',
            'headers': headers
                }
        response = self.send_http(data)
        return response

    @api_call
    def qlf_shop_product_api(self, shopId:int, categoryId:int, issueType:str, materialId:int):
        """
        新增店铺商品
        """
        headers = self.headers
        headers['Authorization'] = self.get_Authorization
        api = self.conf_data['api']['admin']['qlf_shop_product']
        data = {
            'url': self.host + api,
            'method': 'post',
            'headers': headers,
            'json' : {
                    'shopId' : shopId,
                    'categoryId' : categoryId,
                    'issueType' : issueType,
                    'materialId' : materialId
                    }
                }
        response = self.send_http(data)
        return response

    @api_call
    def qlf_shop_product_template_api(self, id:int, userId:int):
        """
        模版ID查询模版商品信息
        """
        headers = self.headers
        headers['Authorization'] = self.get_Authorization
        api = self.conf_data['api']['admin']['qlf_shop_product_template']
        index = api.find('?')
        api = api[:index] + str(id) + api[index+1:]
        data = {
            'url': self.host + api,
            'method': 'get',
            'headers': headers,
            'params' : {
                        'id' : id,
                        'userId' : userId
                        }
                }
        response = self.send_http(data)
        return response

    @api_call
    def qlf_shop_category_id_api(self, id:int, categoryName:str):
        """
        更新分类
        """
        headers = self.headers
        headers['Authorization'] = self.get_Authorization
        api = self.conf_data['api']['admin']['qlf_shop_category_id']
        index = api.find('?')
        api = api[:index] + str(id) + api[index+1:]
        data = {
            'url': self.host + api,
            'method': 'put',
            'headers': headers,
            'json' : {
                        'categoryName' : id
                        }
                }
        response = self.send_http(data)
        return response

    @api_call
    def qlf_shop_ref_export_api(self, id:int):
        """
        店铺商品出售记录导出
        """
        headers = self.headers
        headers['Authorization'] = self.get_Authorization
        api = self.conf_data['api']['admin']['qlf_shop_ref_export']
        index = api.find('?')
        api = api[:index] + str(id) + api[index+1:]
        data = {
            'url': self.host + api,
            'method': 'get',
            'headers': headers
                }
        import requests
        from loguru import logger
        try:
            response = requests.request(**data)
            logger.info(f"响应状态码为：{response.status_code}")
        except Exception as e:
            logger.error(f'发送请求失败，请求参数为：{data}')
            logger.exception(f'发生的错误为：{e}')
            raise e
        else:
            return response

    @api_call
    def product_find_indexconfig_api(self, pageNumber:int, pageSize:int, configType:str):
        """
        首页配置列表
        """
        headers = self.headers
        headers['Authorization'] = self.get_Authorization
        api = self.conf_data['api']['admin']['product_find_indexconfig']
        data = {
            'url': self.host + api,
            'method': 'get',
            'headers': headers,
            'json' : {
                        'pageNumber' : pageNumber,
                        'pageSize' : pageSize,
                        'configType' : configType
                        }
                }
        response = self.send_http(data)
        return response

    @api_call
    def category_query_api(self):
        """
        首页配置查询所有分类
        """
        headers = self.headers
        headers['Authorization'] = self.get_Authorization
        api = self.conf_data['api']['admin']['category_query']
        data = {
            'url': self.host + api,
            'method': 'get',
            'headers': headers
                }
        response = self.send_http(data)
        return response

    @api_call
    def category_updateCategory_api(self, id:int, name:str, coverUrl:str, orderNum:int):
        """
        首页配置新增/修改分类        
        """
        headers = self.headers
        headers['Authorization'] = self.get_Authorization
        api = self.conf_data['api']['admin']['category_updateCategory']
        data = {
            'url': self.host + api,
            'method': 'post',
            'headers': headers,
            'json' : {
                    'id' : id,
                    'name' : name,
                    'coverUrl' : coverUrl,
                    'orderNum' : orderNum
                    }
                }
        response = self.send_http(data)
        return response

    @api_call
    def constantConfig_constantList_api(self):
        """
        首页配置查询所有QRcode        
        """
        headers = self.headers
        headers['Authorization'] = self.get_Authorization
        api = self.conf_data['api']['admin']['constantConfig_constantList']
        data = {
            'url': self.host + api,
            'method': 'get',
            'headers': headers
                }
        response = self.send_http(data)
        return response

    @api_call
    def constantConfig_updateConstantConfig_api(self, id:int, configValue:str):
        """
        首页配置新增/修改QRcode
        """
        headers = self.headers
        headers['Authorization'] = self.get_Authorization
        api = self.conf_data['api']['admin']['constantConfig_updateConstantConfig']
        data = {
            'url' : self.host + api,
            'method' : 'post',
            'headers' : headers,
            'json' : {
                    'id' : id,
                    'configValue' :configValue
                    }
                }
        response = self.send_http(data)
        return response

    def guide_list_api(self):
        """
        首页配置查询所有QRcode        
        """
        headers = self.headers
        headers['Authorization'] = self.get_Authorization
        api = self.conf_data['api']['admin']['guide_list']
        data = {
            'url': self.host + api,
            'method': 'get',
            'headers': headers
                }
        response = self.send_http(data)
        return response